package drools;

import com.alibaba.fastjson.JSON;
import com.irdstudio.biz.drools.beans.RuleBean;
import com.irdstudio.biz.drools.beans.RuleBean.RESULT;
import com.irdstudio.biz.drools.common.BizRuleAgendaFilter;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.commons.io.IOUtils;
import org.kie.api.runtime.KieSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class DroolsRuleTestBase extends TestCase {

    Logger log = LoggerFactory.getLogger(this.getClass());

    public RuleBean getRuleBeanFromJson(String ruleId) {
        InputStream jsonInput = this.getClass().getResourceAsStream(ruleId + ".json");
        Assert.assertNotNull(jsonInput);
        String json = null;
        try {
            json = IOUtils.toString(jsonInput, "UTF-8");
        } catch (IOException e) {
            log.error("读取json异常", e);
        }
        RuleBean param = JSON.parseObject(json, RuleBean.class);
        Assert.assertNotNull(param);
        return param;
    }

    public void after(RuleBean param, BizRuleAgendaFilter filter) {
        Assert.assertNotNull(filter);
        // 处理结果
        Set<String> acceptSet = new LinkedHashSet<String>(filter.getRuleSet());
        // 取差集 得到执行通过的规则
        acceptSet.removeAll(param.getRefuseSet());
        for (String ruleId : acceptSet) {
            param.setResult(ruleId, RESULT.ACCEPT, "通过");
        }

        //log.info("fireRule result: \n{}", JSON.toJSONString(param, true));
        log.info("fireRule result refuseSet: {}", param.getRefuseSet());
        log.info("fireRule result acceptSet: {}", param.getAcceptSet());
    }


    public void testRuleById(String ruleId) {
        RuleBean param = getRuleBeanFromJson(ruleId);
        KieSession session = DroolsUtils.getKieSession();
        param.setCurrentRuleCollId(ruleId);
        session.insert(param);
        BizRuleAgendaFilter filter = new BizRuleAgendaFilter(Arrays.asList(ruleId));
        session.fireAllRules(filter);
        session.dispose();
        log.info("============ ruleId : {}", ruleId);
        log.info("============ param : {}", JSON.toJSONString(param.getParam()));
        log.info("============ execu : {}", JSON.toJSONString(param.getExecu()));
        log.info("============ Output : {}", JSON.toJSONString(param.getOutput()));
        after(param, filter);
    }

    public void testRuleByCollection(String jsonName, List<String> ruleIds) {
        RuleBean param = getRuleBeanFromJson(jsonName);
        KieSession session = DroolsUtils.getKieSession();
        param.setCurrentRuleCollId(jsonName);
        session.insert(param);
        if (ruleIds == null) {
            session.fireAllRules();
        } else {
            BizRuleAgendaFilter filter = new BizRuleAgendaFilter(ruleIds);
            session.fireAllRules(filter);
        }
        session.dispose();
        log.info("============ param : {}", JSON.toJSONString(param.getParam()));
        log.info("============ execu : {}", JSON.toJSONString(param.getExecu()));
        log.info("============ Output : {}", JSON.toJSONString(param.getOutput()));
        //after(param, filter);
    }

    public String getInvokeMethodName() {
        StackTraceElement[] temp = Thread.currentThread().getStackTrace();
        StackTraceElement a = (StackTraceElement) temp[2];
        return a.getMethodName().replaceFirst("test_", "");
    }
}
